<?php
/* define base dir */
define('_kmm_base_dir_',			realpath(dirname(__FILE__).'/..'));

/* get settings parameters */
require_once(_kmm_base_dir_.'/content/settings.inc.php');

/* directories building */
define('_kmm_core_dir_',			_kmm_base_dir_ . '/core');
define('_kmm_content_dir_',		_kmm_base_dir_ . '/content');
define('_kmm_music_dir_', _kmm_base_dir_.'/'._kmm_music_from_project_dir_);
define('_kmm_view_dir_',		_kmm_core_dir_ .'/default_view');


/* URI's building */
define('_kmm_base_uri_',			_kmm_access_protocol_.$_SERVER['SERVER_NAME']._kmm_project_from_serveur_root_path_);
define('_kmm_core_uri_',			_kmm_base_uri_ . '/core');
define('_kmm_content_uri_',		_kmm_base_uri_ . '/content');
define('_kmm_view_uri_',		str_replace(_kmm_base_dir_, _kmm_base_uri_, _kmm_view_dir_));
define('_kmm_img_uri_',				_kmm_view_uri_ . '/img');
define('_kmm_css_uri_',				_kmm_view_uri_ . '/css');
define('_kmm_js_uri_',				_kmm_view_uri_ . '/js');
define('_kmm_jquery_uri_',			_kmm_core_uri_ . '/lib/jquery');
define('_kmm_fff_uri_',				_kmm_core_uri_ . '/lib/famfamfam_silk_icons_v013/icons');
define('_kmm_music_uri_', str_replace(_kmm_base_dir_, _kmm_base_uri_, _kmm_music_dir_));

/* autoload classes */
require_once(_kmm_core_dir_.'/lib/kasifi-listing/Listing.php');
/*
function __autoload($className)
{
	if (!class_exists($className, false))
		require_once(_kmm_base_dir_.'/classes/'.$className.'.php');
}
*/

/* set ajax mode */
$ajax_mode = false;
if (isset($force_ajax_mode) && $force_ajax_mode === true)
	$ajax_mode = true;

require_once('functions.inc.php');

/* librairies */
require_once("lib/php-reader-2008-07-29/ID3v2.php");
require_once("lib/php-reader-2008-07-29/ID3/Frame/TIT2.php");
require_once('lib/smarty-2.6.20/libs/Smarty.class.php');

/* steps settings */
$steps = array(
	'downloaded' => array(
		'name' => 'downloaded',
		'dir' => _kmm_music_dir_ . '/1_downloaded',
		'columns' => array('checkbox','filename','trash'),
		'tag_columns' => array('TIT2','TPE1','TCON','TALB','TRCK','TRCK'),
		'treeView' => true
	),
	'selected' => array(
		'name' => 'selected',
		'dir' => _kmm_music_dir_ . '/2_selected',
		'columns' => array('checkbox','filename','trash'),
		'tag_columns' => array('TIT2','TPE1','TCON'),
		'treeView', true
	),
	'retagged' => array(
		'name' => 'retagged',
		'dir' => _kmm_music_dir_ . '/3_retagged',
		'columns' => array('checkbox','filename','trash'),
		'tag_columns' => array('TIT2','TPE1','TCON'),
		'treeView' => true
	),
	'undoubled' => array(
		'name' => 'undoubled',
		'dir' => _kmm_music_dir_ . '/4_undoubled',
		'columns' => array('checkbox','filename','trash'),
		'tag_columns' => array('TIT2','TPE1','TCON'),
		'treeView' => false
	),
	'normalized' => array(
		'name' => 'normalized',
		'dir' => _kmm_music_dir_ . '/5_normalized',
		'columns' => array('checkbox','filename','trash'),
		'tag_columns' => array('TIT2','TPE1','TCON'),
		'treeView' => false
	),
	'bpm_added' => array(
		'name' => 'bpm_added',
		'dir' => _kmm_music_dir_ . '/6_bpm_added',
		'columns' => array('checkbox','filename','trash'),
		'tag_columns' => array('TIT2','TPE1','TCON'),
		'treeView' => false
	),
	'lyrics_added' => array(
		'name' => 'lyrics_added',
		'dir' => _kmm_music_dir_ . '/7_lyrics_added',
		'columns' => array('checkbox','filename','trash'),
		'tag_columns' => array('TIT2','TPE1','TCON'),
		'treeView' => false
	),
	'image_added' => array(
		'name' => 'image_added',
		'dir' => _kmm_music_dir_ . '/8_image_added',
		'columns' => array('checkbox','filename','trash'),
		'tag_columns' => array('TIT2','TPE1','TCON'),
		'treeView' => false
	),
	'cleaned' => array(
		'name' => 'cleaned',
		'dir' => _kmm_music_dir_ . '/9_cleaned',
		'columns' => array('checkbox','filename','trash'),
		'tag_columns' => array('TIT2','TPE1','TCON'),
		'treeView' => false
	)
);

/* Tag properties */
$tagProperties = array(
	'TIT2' => array(
		'name' => 'Title ID3v2',
		'width' => '16'
	),
	'TPE1' => array(
		'name' => 'Artist ID3v1',
		'width' => '22'
	),
	'TCON' => array(
		'name' => 'Genre',
		'width' => '12'
	),
	'TPE2' => array(
		'name' => 'Artist ID3v2',
		'width' => '15'
	),
	'TALB' => array(
		'name' => 'Album ID3v2',
		'width' => '15'
	),
	'TDRC' => array(
		'name' => 'Date ID3v2',
		'width' => '15'
	),
	'TPUB' => array(
		'name' => 'Publisher ID3v2',
		'width' => '10'
	)
);

/* get actual step */
if (isset($_GET['step']) AND $_GET['step'] != '')
	$stepName = $_GET['step'];
else
	$stepName = 'downloaded';
$currentStep = &$steps[$stepName];

/* base directory rights checks */
if (!file_exists(_kmm_music_dir_) OR !is_readable(_kmm_music_dir_))
	_display_error('The base directory '._kmm_music_dir_.' doesn\'t exists or apache is not allowed to access it.');

/* adding useful data for each step */
foreach ($steps as $stepName => &$step)
{
	/* folder rights checking */
	if (!file_exists($step['dir']) OR !is_readable($step['dir']))
		_display_error('The directory '.$step['dir'].' doesn\'t exists or apache is not allowed to access it.');
	if(!_is_readable($step['dir'], true) OR !_is_writable($step['dir'], true))
		_display_error('The directory '.$step['dir'].' contains some files or folders witch are not recursively 777 rights.');
	/* tracking listing */
	$step['tracks'] = _scanDirectories($step['dir'],array("mp3"));
	/* uri constructing */
	$step['link_uri'] = _kmm_base_uri_.'?step='.$stepName;
}

require_once('smarty.config.inc.php');

$smarty->assign(array(
	'steps' => $steps,
	'currentStep' => $currentStep,
	'currentStepName' => $currentStep['name']
));
